#!/usr/bin/env python
#-*- coding:utf-8 -*-

import sys
import os
ROOT_PATH = os.path.dirname(os.path.realpath(__file__))
sys.path.append(ROOT_PATH)
from config import utils
from config.utils import (_imp_module, get_app_settings,
                          get_settings, set_database, set_database_session)
import sqlalchemy
from sqlalchemy.orm import sessionmaker

if len(sys.argv) > 1:
    run_level = sys.argv[1]
else:
    run_level = 'development'
utils.RUN_LEVEL = run_level
utils.ROOT_PATH = ROOT_PATH


databases = get_settings("databases", "")
for d in databases:
    engine = sqlalchemy.create_engine(databases[d], echo=True)
    Session = sessionmaker(bind=engine)
    session = Session()
    set_database(d, engine)
    set_database_session(d, session)


root_urls = _imp_module("config.routes.root_urls")
app_settings = get_app_settings()
